//
//  MDDateTansformer.m
//  MideaWorld_Zizhu
//
//  Created by Minus on 7/17/15.
//  Copyright (c) 2015 Minus. All rights reserved.
//

#import "MDDateTansformer.h"

@implementation MDDateTansformer

- (NSString *)toUTCWithNSString:(NSString *)time
{
    NSArray *array = [time componentsSeparatedByString:@":"];
    NSInteger hour = [array[0] integerValue];
    NSInteger minute = [array[1] integerValue];
    
    NSDate *date = [NSDate date];
    
    //设置源日期时区
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
    //设置转换后的目标日期时区
    NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
    NSLog(@"%@", destinationTimeZone);
    //得到源日期与世界标准时间的偏移量
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:date];
    //目标日期与本地时区的偏移量
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:date];
    //得到时间偏移量的差值
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    NSInteger intervalHour = interval / 3600;
    
    hour -= intervalHour;
    
    if (hour >= 24)
    {
        hour -= 24;
    }
    else if (hour < 0)
    {
        hour += 24;
    }
    
    return [NSString stringWithFormat:@"%02d:%02d", (int)hour, (int)minute];
}

- (NSString *)toLocalWithNSString:(NSString *)time
{
    NSArray *array = [time componentsSeparatedByString:@":"];
    NSInteger hour = [array[0] integerValue];
    NSInteger minute = [array[1] integerValue];
    
    NSDate *date = [NSDate date];
    
    //设置源日期时区
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
    //设置转换后的目标日期时区
    NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
    NSLog(@"%@", destinationTimeZone);
    //得到源日期与世界标准时间的偏移量
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:date];
    //目标日期与本地时区的偏移量
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:date];
    //得到时间偏移量的差值
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    NSInteger intervalHour = interval / 3600;
    
    hour += intervalHour;
    
    if (hour >= 24)
    {
        hour -= 24;
    }
    else if (hour < 0)
    {
        hour += 24;
    }
    
    return [NSString stringWithFormat:@"%02d:%02d", (int)hour, (int)minute];
}

@end
